// pages/prescription/prescription.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: '',
    showUseDrug: false,
    showEmpty: false,
    did: 0,
    openPid: 0,
    prescriptions: []
  },

  async getPrescriptions() {
    // 请求临期药品列表
    const res = await app.call({
      path: '/prescription/getPrescriptions',
      method: 'GET'
    })
    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      // 按照日期（从早到晚）排序
      const prescriptions = res.data.prescriptions
      prescriptions.sort(function (a, b) {
        return new Date(b.date) - new Date(a.date);
      });
      this.setData({
        prescriptions: prescriptions,
      })
      if(this.data.activeName==''){
        this.setData({
          activeName: this.data.prescriptions[0].pid
        })
      }
    } else {
      Toast.fail(res.errorMsg);
    }

    this.setData({
      showEmpty: this.data.prescriptions.length === 0,
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
    this.getPrescriptions()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  onChange(event) {
    this.setData({
      activeName: event.detail,
      openPid: event.detail
    });
  },

  openDialog(event) {
    console.log(event)
    const did = event.currentTarget.id
    this.setData({
      showUseDrug: true,
      did: did
    })
  },

  async onUseDrug() {
    // 修改药品余量
    const res = await app.call({
      path: '/drug/updateDrugSurplus',
      method: 'PUT',
      data: {
        id: this.data.did,
        surplus: this.data.value
      }
    })

    console.log(res)
    if (res.code === 0) {
      Toast.success("修改成功");
      this.getPrescriptions()
    } else {
      Toast.fail(res.errorMsg);
    }
    this.setData({
      showUseDrug: false,
    })
  },

  closeDialog() {
    this.setData({
      showUseDrug: false
    })
  },

  delPrescription(event) {
    Dialog.confirm({
      message: '确定删除？',
    }).then(async () => {
      console.log(event)
      const pid = event.currentTarget.dataset.pid
      // 删除医嘱
      const res = await app.call({
        path: 'prescription/deletePrescription?pid=' + pid,
        method: 'DELETE',
      })
      console.log(res)
      if (res.code === 0) {  
        this.getPrescriptions()
        Toast.success("删除成功");
      } else {
        Toast.fail(res.errorMsg);
      }
    }).catch(() => {
      // on cancel
    });
  },

  async onChangeRecent(event) {
    console.log(event)
    const pid = event.currentTarget.dataset.pid
    const recent = event.currentTarget.dataset.recent
    // 修改近期使用
    const res = await app.call({
      path: '/prescription/updateRecent',
      method: 'PUT',
      data: {
        pid: pid,
        recent: recent == 1 ? 0 : 1
      }
    })
    console.log(res)
    if (res.code === 0) {
      Toast.success("修改成功");
      this.getPrescriptions()
    } else {
      Toast.fail(res.errorMsg);
    }
  },

  addPrescription() {
    wx.navigateTo({
      url: '/pages/editPrescription/editPrescription?from=addPrescription',
    })
  }

})